
import scrapy
import json
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exceptions import NotConfigured
from scrapy.pipelines.images import ImagesPipeline

from doubanbook.mylib.mypymysql import mypymysql
class cctvItemPipeline(object):

    def __init__(self, file_name=None):
        self.file_name = 'cctv.json'
        self.fp = None
        self.mypy = mypymysql()

    def open_spider(self, spider):
        self.fp = open(self.file_name, 'w', encoding='utf-8')

    def close_spider(self, spider):
        self.fp.close()

    def process_item(self, item, spider):
        json_item = json.dumps(dict(item))
        try:
            self.fp.write(json_item + "\n")
            self.mypy.mydateadd_ctv10(location=item['location'], detailsid=item['detailsid'],
                                      imagelink=item['imagelink'],
                                      playtime=item['playtime'], duration=item['duration'], title=item['title'],
                                      album_order_id=item['album_order_id'], targetpage=item['targetpage'])
        except:
            print('process_item')
        return item



